<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_yuejie/model')->方法名();
*/
namespace jy_yuejie;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function getmids(){
		$mids=pdo_getall('jy_weishop_member',array('yuejie_type'=>array(1,2),'uniacid'=>$_SESSION['uniacid']),array('mid'),'mid');
		if($mids){
			$mids=array_keys($mids);
		}else{
			$mids=-1;
		}
		return $mids;
	}
	public function getworkermids($adminmid=0,$nomy=false){
		$mids=pdo_getall('jy_yuejie_worker',array('adminmid'=>$adminmid,'uniacid'=>$_SESSION['uniacid']),array('mid'),'mid');
		if(!$nomy){
			$mids[$adminmid]=$mids[$adminmid]?:array('mid'=>$adminmid);
		}
		if($mids){
			$mids=array_keys($mids);
		}else{
			$mids=-1;
		}
		return $mids;
	}
	public function up_lastmonth($worker=array()){
		$yuestart = mktime(0,0,0,date('m')-1,1,date('Y'));
		$yueend = mktime(0,0,0,date('m'),1,date('Y'));
		$time=$yuestart+200;
		$jielog=pdo_get('jy_yuejie_jielog',array('mid'=>$worker['mid'],'month'=>date('Y-m',$time),'uniacid'=>$_SESSION['uniacid']));
		if($jielog['id']){
			return $jielog;
		}
		$mids=$this->getmids();
		$orderprice=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mids,'yuejiemid'=>$worker['mid'],'createtime between'=>array($yuestart,$yueend)),'price');
		$ordernum=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mids,'yuejiemid'=>$worker['mid'],'createtime between'=>array($yuestart,$yueend)));
		$jielog=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$worker['mid'],
				'orderprice'=>$orderprice,
				'ordernum'=>$ordernum,
				'month'=>date('Y-m',$time),
				'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_yuejie_jielog',$jielog);
		return $jielog;
	}
	public function addorder($order=array(),$status=0,$plugin='',$yuejiemid=0){
		$isaddid=pdo_getcolumn('jy_wxshifu_order',array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid'],'plugin'=>$plugin),'id');
		if($isaddid){
			return $isaddid;
		}
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$order['mid'],
				'orderid'=>$order['id'],
				'yuejiemid'=>$yuejiemid,
				'price'=>$order['price'],
				'plugin'=>$plugin,
				'status'=>$status,
				'createtime'=>TIMESTAMP,
		);
		$this->insert('order',$data);
		return $this->insertid();
	}
	public function checktype($member=array()){
		if(!$this->setting['yuejie_open']){
			return 0;
		}
		if(!$member['mid']){
			$member=pdo_get('jy_weishop_member',array('merchid'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		}
		if($member['yuejie_type']==2){
			$adminmid=pdo_getcolumn('jy_yuejie_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'adminmid');
			$member=pdo_get('jy_weishop_member',array('merchid'=>0,'mid'=>$adminmid,'uniacid'=>$_SESSION['uniacid']));
		}
		if($member['yuejie_type'] && $member['yuejie_endtime']>TIMESTAMP){
			return $member['mid'];
		}else{
			return 0;
		}
	}
}
?>